package com.cskaoyan;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GenerateData
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2022/1/14 14:43
 * @Version V1.0
 **/
public class GenerateData {

    private static List<String> ips = new ArrayList<>();

    private static List<String> dates = new ArrayList<>();

    private static List<String> urls = new ArrayList<>();

    private static List<String> devices = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        //数据的格式：ip地址 访问时间 访问地址 访问设备
        prepare();
        StringBuffer buffer = new StringBuffer();
        String path = GenerateData.class.getClassLoader().getResource("access.log").getPath();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path), true)));
        for (int i = 0; i < 1000000; i++) {
            buffer.append(ListUtil.getRandomElement(ips) + "---");
            buffer.append(dates.get(i) + "---");
            buffer.append(ListUtil.getRandomElement(urls) + "---");
            buffer.append(ListUtil.getRandomElement(devices));
            bufferedWriter.write(buffer.toString());
            bufferedWriter.newLine();
            buffer.delete(0, buffer.length());
        }
        bufferedWriter.flush();
        bufferedWriter.close();
    }

    private static void prepare() throws IOException {
        loadIPs();
        loadDates();
        loadUrls();
        loadDevices();
    }

    private static void loadDevices() throws IOException {
        String path = GenerateData.class.getClassLoader().getResource("device.txt").getPath();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(path)));
        String ip = null;
        while ((ip = bufferedReader.readLine()) != null){
            devices.add(ip);
        }

    }

    private static void loadUrls() throws IOException {
        String path = GenerateData.class.getClassLoader().getResource("url.txt").getPath();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(path)));
        String ip = null;
        while ((ip = bufferedReader.readLine()) != null){
            urls.add(ip);
        }

    }

    private static void loadDates() throws IOException {
        String path = GenerateData.class.getClassLoader().getResource("date.txt").getPath();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(path)));
        String ip = null;
        while ((ip = bufferedReader.readLine()) != null){
            dates.add(ip);
        }

    }

    private static void loadIPs() throws IOException {
        String path = GenerateData.class.getClassLoader().getResource("ip.txt").getPath();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(path)));
        String ip = null;
        while ((ip = bufferedReader.readLine()) != null){
            ips.add(ip);
        }
    }
}
